package com.card323.crpg;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Hashtable;

import com.card323.crpg.battle.ActionPoint;
import com.card323.crpg.battle.HeroBattleInfo;
import com.card323.crpg.battle.buff.Buff;
import com.card323.crpg.hero.HeroStatusListener;
import com.card323.crpg.io.MessageReceiver;
import com.card323.crpg.io.MessageSender;
import com.card323.crpg.item.Item;
import com.card323.crpg.item.ItemNull;
import com.card323.crpg.shop.DefaultShop;
import com.card323.crpg.shop.Shop;
import com.card323.crpg.skill.Skill;

public class Hero implements MessageSender, MessageReceiver {

	private MessageSender _msender;
	private MessageReceiver _mreceiver;

	private String _name;
	public boolean _is_ai;
	public int _base_attack;
	public int _random_attack;
	public int _random_attack_times;
	private int _life;
	private int _current_life;
	public int _money;

	private HeroBattleInfo _bi;
	public ActionPoint _attack_speed;
	public String _email;

	private ArrayList<Item> _items = new ArrayList<Item>();
	public Hashtable<String, Skill> _skills = new Hashtable<String, Skill>();
	public ActionPoint _action_speed;
	private Shop _shop;
	private HeroTeam _team;

	public boolean is_null_hero() {
		return get_name() == null;
	}

	public Hero(String email) {
		_email = email;
		_action_speed = new ActionPoint(5, 0, 0, 0, 0, 0);
		_shop = new DefaultShop();
		_money = 100;
		_team = new HeroTeam(this);
		for (int i = 0; i < 10; i++)
			_items.add(new ItemNull());
		_bi = new HeroBattleInfo(this);
	}

	public Item get_item(int index) {
		return _items.get(index);
	}

	public String get_info() {
		StringBuffer sb = new StringBuffer();
		sb.append("[" + get_name() + "]\n");
		sb.append("伤害:" + _base_attack + "+" + _random_attack + "d"
				+ _random_attack_times + "\n");
		sb.append("生命值:" + get_current_life() + "/" + get_life() + "\n");
		sb.append("Money:" + _money + "\n");
		sb.append("*提示:输入?i以显示背包信息");
		return sb.toString();
	}

	public boolean is_died() {

		return get_current_life() <= 0;
	}

	// io
	void set_msender(MessageSender _msender) {
		this._msender = _msender;
	}

	void set_mreceiver(MessageReceiver mreceiver) {
		_mreceiver = mreceiver;

	}

	public String get_message() {
		return _mreceiver.get_message();

	}

	public void send_message(String msg) {
		if (!_is_ai)
			_msender.send_message(msg);
	}

	public void send_message(String msg, Color c) {
		if (!_is_ai)
			_msender.send_message(msg, c);

	}

	// item
	public void set_item(int i, Item item) {
		_items.set(i, item);

	}

	public void set_null_item(int i) {
		_items.set(i, new ItemNull());
	}

	public void swap_item(int item_index1, int item_index2) {
		Item item1 = get_item(item_index1);
		Item item2 = get_item(item_index2);
		set_item(item_index1, item2);
		set_item(item_index2, item1);

	}

	public void remove_item(int item_index) {
		set_null_item(item_index);
	}

	public Shop get_shop() {
		return _shop;

	}

	public void buy_item(Item item) {
		_money -= item.get_price();
		int index = get_null_item_index();
		set_item(index, item);
	}

	private int get_null_item_index() {
		for (int i = 1; i <= 10; i++) {
			int index = i % 10;
			Item item = get_item(index);
			if (item.is_null_item())
				return index;

		}
		return -1;
	}

	public boolean is_package_full() {
		for (Item i : _items) {
			if (i.is_null_item())
				return false;
		}
		return true;
	}

	// battle
	public HeroTeam get_team() {
		return _team;
	}

	public void add_skill(String key, Skill skill) {
		_skills.put(key, skill);

	}

	public void remove_skill(String key) {
		_skills.remove(key);
	}

	public void replace_skill(String key, Skill new_skill) {
		remove_skill(key);
		add_skill(key, new_skill);
	}

	public String get_skill_info() {
		String[] keys = { "q", "w", "e", "r", "t" };
		StringBuffer sb = new StringBuffer();
		sb.append("技能信息:\n");
		for (String key : keys) {
			Skill skill = _skills.get(key);
			if (skill != null) {
				String skill_name = skill._name;
				String skill_info = skill.get_info();
				sb.append(key + ": " + skill_name + ":" + skill_info + "\n");
			} else {
				sb.append(key + ":未设置\n");
			}
		}
		return sb.toString();
	}

	public String get_skill_info(String key) {
		StringBuffer sb = new StringBuffer();
		sb.append("技能信息:\n");
		Skill skill = _skills.get(key);
		if (skill != null) {
			String skill_name = skill._name;
			String skill_info = skill.get_info();
			sb.append(key + ": " + skill_name + ":" + skill_info + "\n");
		} else {
			sb.append(key + ":未设置\n");
		}
		return sb.toString();
	}

	public void add_buff(Buff _buff) {
		get_bi().add_buff(_buff);

	}

	public String get_name() {
		return _name;
	}

	ArrayList<HeroStatusListener> _hero_status_listener_list = new ArrayList<HeroStatusListener>();

	public void add_hero_status_listener(HeroStatusListener listener) {
		_hero_status_listener_list.add(listener);
	}

	public void set_name(String name) {

		for (HeroStatusListener listener : _hero_status_listener_list) {
			listener.on_name_changed(get_name(), name);
		}
		this._name = name;
	}

	public int get_life() {
		return _life;
	}

	public void set_life(int life) {
		for (HeroStatusListener listener : _hero_status_listener_list) {
			listener.on_hp_changed(get_life(), life);
		}
		this._life = life;
	}

	public HeroBattleInfo get_bi() {
		return _bi;
	}

	public boolean is_in_battle() {
		return get_bi().is_in_battle();
	}

	public void send_error_message(String message) {
		send_message(message, Color.RED);

	}

	public void set_allies_listener() {
		ListenerProvider provider = PlayerManager.getInstance()
				.get_listener_provider(_email);
		if (provider != null) {
			add_hero_status_listener(provider.get_allies_status_listener());
			get_bi().get_ap().add_hero_ap_listener_list(
					provider.get_allies_ap_listener());
		}

	}

	public void set_enemy_listener(Hero enemy) {
		ListenerProvider provider = PlayerManager.getInstance()
				.get_listener_provider(_email);
		if (provider != null) {
			enemy.add_hero_status_listener(provider.get_enemy_status_listener());
			enemy.get_bi()
					.get_ap()
					.add_hero_ap_listener_list(provider.get_enemy_ap_listener());
		}
	}

	public int get_current_life() {
		return _current_life;
	}

	public void set_current_life(int current_life) {
		if (current_life < 0)
			current_life = 0;
		for (HeroStatusListener listener : _hero_status_listener_list) {
			listener.on_current_hp_changed(get_current_life(), current_life);
		}
		this._current_life = current_life;
	}

}
